import { useSelector } from 'react-redux'
import { useLocation } from 'react-router-dom'

import { RootState } from '@/store'
import { findRouteBySearch } from '@/router'
import dictionary from '@/router/dictionary'

interface IAccess {
  action?: string[]
  view?: string[]
}

export default function useAccess(): IAccess {
  const location = useLocation()
  const currentRoute = findRouteBySearch({ key: location.pathname, type: 'path' }, dictionary)
  const currentAccess = useSelector((state: RootState) => state.access)
  let access = {}
  if (currentRoute) {
    let [first] = currentAccess.filter((v) => v.key === currentRoute.key)
    access = {
      view: first.view,
      action: first.action
    }
  }
  return access
}
